<?php

namespace App\Http\Controllers\Admin;

use App\Models\Common\CcBlacklist;
use App\Models\Common\CcSetup;
use App\Models\Common\TrafficStatistics;
use App\Models\Company\CompanyTrialError;
use App\Models\Personal\PersonalTrialError;
use Illuminate\Http\Request;
use Style, Cache, DB;

class SafetyController extends CommonController{
    /**
     * 防CC攻击设置
     * @return mixed
     */
    public function ccSet(){
        $ccSetup = CcSetup::where('sign', 'pc')->first();
        $number = (!empty($ccSetup->number)) ? $ccSetup->number : '';
        return Style::view('admin.safety.cc_set', compact('ccSetup', 'number'));
    }

    /**
     * 防CC攻击设置
     * @return mixed
     */
    public function postCcSet(Request $request){
        $input = $request->only(['statistics', 'for_blacklist', 'intelligent', 'number', 'sign']);
        $ccSetup = CcSetup::where('sign', 'pc')->first();
        if(!empty($ccSetup)){
            CcSetup::where('id', $ccSetup->id)->update($input);
            flash('更新防CC攻击PC设置成功', 'success');
            Cache::forget('cc_setup');
            return redirect('admin/cc_set');
        }else{
            CcSetup::create($input);
            flash('添加防CC攻击PC设置成功', 'success');
            return redirect('admin/cc_set');
        }
    }

    /**
     * 防CC攻击API设置
     * @return mixed
     */
    public function ccApiSet(){
        $ccSetup = CcSetup::where('sign', 'api')->first();
        $number = (!empty($ccSetup->number)) ? $ccSetup->number : '';
        return Style::view('admin.safety.cc_api_set', compact('ccSetup', 'number'));
    }

    /**
     * 防CC攻击API设置
     * @return mixed
     */
    public function postApiCcSet(Request $request){
        $input = $request->only(['statistics', 'for_blacklist', 'intelligent', 'number', 'sign']);
        $ccSetup = CcSetup::where('sign', 'api')->first();
        if(!empty($ccSetup)){
            CcSetup::where('id', $ccSetup->id)->update($input);
            Cache::forget('cc_setup_api');
            flash('更新防CC攻击Api设置成功', 'success');
            return redirect('admin/cc_api_set');
        }else{
            CcSetup::create($input);
            flash('添加防CC攻击Api设置成功', 'success');
            return redirect('admin/cc_api_set');
        }
    }

    /**
     * 防CC列表
     * @return mixed
     */
    public function ccList(Request $request){
        $input = arrayFilter($request->only(['keyword', 'minute']));
        $minute = (!empty($input['minute'])) ? $input['minute'] : '';
        $minutes = date('Y-m-d H:i:s', strtotime('-' . $minute . ' Minute', time()));
        $trafficStatistics = DB::table('traffic_statistics')
            ->select('*', DB::raw('count(*) as ccc'))
            ->where('created_at', '>', $minutes)
            ->where(function($query) use ($input){
                $keyword = (!empty($input['keyword'])) ? $input['keyword'] : '';
                if(strlen($keyword) > 0){
                    $query->where('ip', 'like', '%' . $keyword . '%');
                }
            })
            ->groupBy('ip')
            ->orderBy('ccc', 'desc')
            ->paginate(20);
        //分页分配参数 过滤boolean假值
        $appends = arrayFilter($input);
        return Style::view('admin.safety.cc_list', compact('trafficStatistics', 'appends'));
    }

    /**
     * 防CC添加到黑名单
     * @return mixed
     */
    public function ccAdd(Request $request){
        if(!empty($request->ip)){
            $ccBlacklist = CcBlacklist::where('ip', $request->ip)->first();
            if(!empty($ccBlacklist)){
                return response()->json(['status' => 1, 'msg' => '该ip【' . $request->ip . '】已经存在']);
            }else{
                CcBlacklist::create(['ip' => $request->ip]);
                return response()->json(['status' => 0, 'msg' => '添加ip【' . $request->ip . '】到黑名单成功']);
            }
        }else{
            return response()->json(['status' => 1, 'msg' => 'ip不能为空']);
        }
    }

    /**
     * 防CC添加到黑名单
     * @return mixed
     */
    public function ccDel(Request $request){
        if(!empty($request->ip)){
            $ccBlacklist = CcBlacklist::where('ip', $request->ip)->first();
            if(!empty($ccBlacklist)){
                CcBlacklist::where('id', $ccBlacklist->id)->delete();
                return response()->json(['status' => 0, 'msg' => '删除ip【' . $request->ip . '】成功']);
            }else{
                return response()->json(['status' => 1, 'msg' => '该ip【' . $request->ip . '】不存在']);
            }
        }else{
            return response()->json(['status' => 1, 'msg' => 'ip不能为空']);
        }
    }

    /**
     * 获取所有的ip信息
     * @param Request $request
     */
    public function ccAllInfo(Request $request){
        $input = arrayFilter($request->only(['keyword', 'ip']));
        $ip = (!empty($request->ip)) ? $request->ip : '';
        $trafficStatistics = TrafficStatistics
            ::where(function($query) use ($ip){
                if(strlen($ip) > 0){
                    $query->where('ip', 'like', '%' . $ip . '%');
                }
            })->where(function($query) use ($input){
                $keyword = (!empty($input['keyword'])) ? $input['keyword'] : '';
                if(strlen($keyword) > 0){
                    $query->where('ip', 'like', '%' . $keyword . '%')
                        ->orWhere('url', 'like', '%' . $keyword . '%')
                        ->orWhere('header', 'like', '%' . $keyword . '%');
                }
            })
            ->orderBy('created_at', 'desc')
            ->paginate(20);
        //分页分配参数 过滤boolean假值
        $appends = arrayFilter($input);
        return Style::view('admin.safety.cc_all_list', compact('trafficStatistics', 'appends'));
    }


    /**
     * 清空数据
     * @return mixed
     */
    public function ccClear(Request $request){
        $result = DB::table('traffic_statistics')->truncate();
        if(empty($result)){
            return response()->json(['status' => 0, 'msg' => '清空所有数据信息成功']);
        }else{
            return response()->json(['status' => 1, 'msg' => '清空所有数据信息失败']);
        }
    }

    /**
     * 个人试错记录
     * @return mixed
     */
    public function personalTrial(Request $request){
        $keyword = (!empty($request->keyword)) ? $request->keyword : '';
        $personalTrail = PersonalTrialError::where(function($query) use ($keyword){
            if(strlen($keyword) > 0){
                $query->where('personal_login_ip', 'like', '%' . $keyword . '%')
                    ->orWhere('username', 'like', '%' . $keyword . '%');
            }
        })->orderBy('created_at', 'desc')->paginate(20);
        return Style::view('admin.safety.personalTrial', compact('personalTrail'));
    }

    /**
     * 企业试错记录
     * @return mixed
     */
    public function companyTrial(Request $request){
        $keyword = (!empty($request->keyword)) ? $request->keyword : '';
        $companyTrial = CompanyTrialError::where(function($query) use ($keyword){
            if(strlen($keyword) > 0){
                $query->where('company_login_ip', 'like', '%' . $keyword . '%')
                    ->orWhere('username', 'like', '%' . $keyword . '%');
            }
        })->orderBy('created_at', 'desc')->paginate(20);
        return Style::view('admin.safety.companyTrial', compact('companyTrial'));
    }

}
